package com.example.pixelyear.utils;

import android.app.Activity;

import com.example.pixelyear.ui.act.MainActivity;

import com.socks.library.KLog;
import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.Stack;

import androidx.appcompat.app.AppCompatActivity;

/**
 * Package    : com.example.baseutils
 * When I wrote this, only The jade emperor and I understood what I was doing, Now God only knows
 * ClassName  : ActivityListUtils
 * Description: ${TODO}
 * Author     : 坐怀灬不乱
 * Date       : 2018/12/20 9:11
 */
public class ActivityListUtils {
    private static WeakReference<ActivityListUtils> mBaseUtils = null;
    private static Stack<Activity> mActivityList = new Stack<>();


    public static ActivityListUtils getInstance() {
        if (null == mBaseUtils || mBaseUtils.get() == null) {
            synchronized (ActivityListUtils.class) {
                if (null == mBaseUtils || mBaseUtils.get() == null) {
                    mBaseUtils = new WeakReference<>(new ActivityListUtils());
                }
            }
        }
        return mBaseUtils.get();
    }

    public void addActivity(Activity pActivity) {
        mActivityList.add(pActivity);
    }

    public void removeActivity(Activity pActivity) {
        if (pActivity != null) {
            mActivityList.remove(pActivity);
            pActivity.finish();
            pActivity = null;
        }
    }

    //获取栈顶Activity
    @Nullable
    public static Activity getCurrentActivity() {
        if (mActivityList.size() == 0)
            return null;
        else
            return mActivityList.lastElement();
    }

    //获取Act数组长度
    public int getActivityListSize() {
        if (mActivityList != null)
            return mActivityList.size();
        else
            return 0;
    }

    /**
     * 通过类名关闭Act
     *
     * @param className 类名
     */
    public void UseNamefinishAct(String className) {
        Activity activity = null;
        for (Activity _ActivityRef : mActivityList) {
            try {
                if (_ActivityRef.getClass().getName().equals(className)) {
                    activity = _ActivityRef;
                    break;
                }
            } catch (NullPointerException ignored) {
            }
        }
        if (activity != null) {
            mActivityList.remove(activity);
            if (!activity.isDestroyed())
                activity.finish();
        }
    }

    /**
     * 通过类名获取Act
     *
     * @param className 类名
     */
    public static Activity UseNameGetAct(String className) {
        Activity activityWeakReference = null;
        for (Activity _ActivityRef : mActivityList) {
            try {
                if (_ActivityRef.getClass().getName().equals(className)) {
                    activityWeakReference = _ActivityRef;
                    break;
                }
            } catch (NullPointerException ignored) {
            }
        }
        assert activityWeakReference != null;
        return activityWeakReference;
    }

    //关闭全部界面，退出程序
    public void finishAllActivity() {
        for (int i = 1; i <= mActivityList.size(); i++) {
            try {
                KLog.e("yxs", "退出Act名字：" + mActivityList.get(mActivityList.size() - i).getClass().getName());
                mActivityList.get(mActivityList.size() - i).finish();
            } catch (NullPointerException ignored) {
            }
        }
        mActivityList.clear();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    //关闭除MainActivity以外的界面
    public void finishActivity() {
        AppCompatActivity mainActivity = null;
        for (Activity _ActivityRef : mActivityList) {
            try {
                KLog.e("yxs", "类名：" + _ActivityRef.getClass().getName());
                if (_ActivityRef.getClass().getName().equals(MainActivity.class.getName())) {
                    mainActivity = (AppCompatActivity) _ActivityRef;
                    continue;
                }
                _ActivityRef.finish();
            } catch (NullPointerException ignored) {

            }
        }
        mActivityList.clear();
        mActivityList.add(mainActivity);
    }
}
